*******************************************************************************
* Load data
*******************************************************************************

use dataset_gallup, clear

*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

preserve

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
local ///
legend1 legend(order(1 "{fontface monospace:{bf:Confidence in Police}}" ///
	2 "{fontface monospace:{bf:Synthetic Control}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)))
local ///
legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)))
	
}

*******************************************************************************
* Make lags
*******************************************************************************

local lags
forvalues i = 1993(1)2013 {
	egen conf`i'm = mean(conf_) if year == `i', by(target)
	egen conf`i' = mean(conf`i'm), by(target)
	drop conf`i'm
	local lags `lags' conf`i'
}

*******************************************************************************
* Set panel and get the treated ID
*******************************************************************************

encode target, gen(id)
xtset id year
su id if target == "police"

*******************************************************************************
* Synth
*******************************************************************************

synth conf_ `lags', trunit(`r(mean)') trperiod(2014) keep(tmp, replace)
use tmp, clear
erase tmp.dta

*******************************************************************************
* Graph
*******************************************************************************

#delimit;

gr tw
	(line _Y_treated _time, col(black) lwid(thick))
	(line _Y_synthetic _time, col(gray) lwid(thick))
	,
		name(g1, replace)
		xline(2014, lpat(dash))
		ylab(0.2(0.2)0.8, angle(horiz))
		plotregion(style(none))
		ytitle("")
		xtitle("")
		`legend1'	
		xlab(1990(8)2022)		
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

gen d = _Y_treated - _Y_synthetic

#delimit;

gr tw
	(line d _time, col(black) lwid(thick))
	(sc d _time, msym(none))
	,
		name(g2, replace)
		yline(0, lpat(shortdash))
		xline(2014, lpat(dash))
		plotregion(style(none))
		ylab(-.3(0.2)0.5, angle(horiz))
		ytitle("")
		xtitle("")
		xlab(1990(8)2022)		
		`legend2'
		;

#delimit cr

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle')

*******************************************************************************
* Close subgroup loop
*******************************************************************************

restore

}

*******************************************************************************
* Combine graphs
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_gallup_sc.pdf", replace

*******************************************************************************
* Substantive effects
*******************************************************************************

use dataset_gallup, clear

collapse M=conf_ (sd) SD=conf_ if year < 2014, by(target subgroup)

su M SD if target == "police" & subgroup == "Democrat"
su M SD if target != "police" & subgroup == "Democrat"

su M SD if target == "police" & subgroup == "Republican"
su M SD if target != "police" & subgroup == "Republican"

*******************************************************************************
* End
*******************************************************************************
